package cn.studyjava.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import cn.studyjava.common.validator.Add;
import cn.studyjava.mybatis.model.User;
import cn.studyjava.mybatis.vo.LoginUserVO;
import cn.studyjava.service.UserService;
import cn.studyjava.util.Md5Util;

@Controller
public class LoginController {

	@Autowired
	private UserService userService;

	@RequestMapping(value = "index", produces = "text/html")
	public String login(Model model) {
		model.addAttribute("content", "仅仅为了测试");
		return "index";
	}

	@RequestMapping(value = "/v1/login", method = { RequestMethod.POST})
	public String login(Model model, @Validated({ Add.class }) @RequestBody LoginUserVO user) {

		try {
			User us = userService.getUser(user.getUserName());
			if (Md5Util.toMD5(us.getPassword()).equals(us.getPassword())) {
			}
			model.addAttribute("user", us);
			return "index";
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
}
